var utils = (function () {
  var handleParamInUrl = function (url, params) {
    var urlTemp = url
    var reg = /{(\w+)}/ig
    var result = null
    while ((result = reg.exec(url)) != null) {
      var replaceStr = params[result[1]]
      var replacedStr = result[0]
      if (typeof (replaceStr) !== 'undefined' && replaceStr != null) {
        urlTemp = urlTemp.replace(replacedStr, replaceStr)
      } else {
        urlTemp = null
        break
      }
    }
    return urlTemp
  }

  let sliceString = function (str, length) {
    return ('0000' + str).slice(-length)
  }

  let transformTime = function (time) {
    let myDate = new Date(time)
    let Y = sliceString(myDate.getFullYear(), 4)
    let M = sliceString(myDate.getMonth() + 1, 2)
    let D = sliceString(myDate.getDate(), 2)
    let h = sliceString(myDate.getHours(), 2)
    let m = sliceString(myDate.getMinutes(), 2)
    let s = sliceString(myDate.getSeconds(), 2)
    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
  }

  return {
    handleParamInUrl: handleParamInUrl,
    transformTime: transformTime
  }
}())

export default utils
